package com.cq.hd.admin.biz;

import com.cq.hd.admin.dto.ActiveDataTrendStatisticsDto;
import com.cq.hd.admin.dto.OrderDataTrendStatisticsDto;
import com.cq.hd.admin.vo.ActiveDataTrendStatisticsVo;
import com.cq.hd.admin.vo.IndexDataStatisticsVo;
import com.cq.hd.admin.vo.OrderDataTrendStatisticsVo;
import com.cq.hd.admin.vo.TodoDataVo;
import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.constant.OrderNoteConstant;
import com.cq.hd.common.enums.biz.*;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.*;
import com.cq.hd.member.api.dto.BusinessEnterApplyQueryDto;
import com.cq.hd.member.api.dto.WithdrawalBillDto;
import com.cq.hd.member.api.vo.BusinessEnterApplyPageVo;
import com.cq.hd.member.api.vo.BusinessEnterApplyVo;
import com.cq.hd.member.api.vo.WithdrawalBillVo;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.TbOrderItemApi;
import com.cq.hd.order.api.TbOrderSettleBillApi;
import com.cq.hd.order.api.dto.OrderDto;
import com.cq.hd.order.api.dto.OrderItemDto;
import com.cq.hd.order.api.dto.OrderSettleBillDto;
import com.cq.hd.order.api.vo.OrderItemVo;
import com.cq.hd.order.api.vo.OrderSettleBillVo;
import com.cq.hd.order.api.vo.OrderVo;
import com.cq.hd.product.api.TbActiveApi;
import com.cq.hd.product.api.dto.ActiveDto;
import com.cq.hd.product.api.vo.ActivePageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class IndexBiz {

    @Autowired
    private TbOrderApi orderApi;

    @Autowired
    private TbOrderItemApi orderItemApi;

    @Autowired
    private TbAppUserApi appUserApi;

    @Autowired
    private TbBusinessApi businessApi;

    @Autowired
    private TbMerchantApi merchantApi;

    @Autowired
    private TbBusinessAgentApi businessAgentApi;

    @Autowired
    private TbActiveApi activeApi;

    @Autowired
    private TbBusinessEnterApplyApi businessEnterApplyApi;

    @Autowired
    private TbWithdrawalBillApi withdrawalBillApi;

    @Autowired
    private TbOrderSettleBillApi orderSettleBillApi;

    public IndexDataStatisticsVo dataStatistics() {
        // 今日成交额=今天产生的订单的实际支付总额（不包含退款）
        BigDecimal dayOrderAmt = BigDecimal.ZERO;
        // 今日成交量=今天产生的订单数量（不包含退款）
        int dayOrderNum = 0;
        // 今日结算金额=今天完成结算的订单金额总和（包含本金+补贴）
        BigDecimal daySettleAmt = BigDecimal.ZERO;
        // 今日结算佣金=今天完成结算的订单佣金总和
        BigDecimal daySettleSubsidyAmt = BigDecimal.ZERO;
        // 今日提现佣金=今天成功提现的佣金总和
        BigDecimal dayWithdrawalAmt = BigDecimal.ZERO;
        // 今日新增用户数=今天注册的用户数量
        int newUserNum = 0;
        // 今日新增商家数=今天入驻成功的商家数量
        int newBusinessNum = 0;
        // 今日新增商户数=今天入驻成功的商户数量
        int newMerchantNum = 0;
        // 今日新增经纪人数=今天成为经纪人的用户数量
        int newAgentNum = 0;
        // 累计发布活动数
        int totalActiveNum = 0;
        // 本周上新活动数
        int weekActiveNum = 0;

        LocalDate now = LocalDate.now();
        String nowDateStr = LocalDateUtil.toLocalDateToString(now, Constant.YYYY_MM_DD);
        String startTime = nowDateStr + " 00:00:00";
        String endTime = nowDateStr + " 23:59:59";

        // 根据日期时间查询订单项数据，获取今日订单金额，今日订单数
        OrderDto orderDto = new OrderDto();
        orderDto.setStartOrderTime(startTime);
        orderDto.setEndOrderTime(endTime);
        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if(!CollectionUtils.isEmpty(orderVos)){
            for (OrderVo orderVo : orderVos) {
                BigDecimal payPrice = orderVo.getPayPrice();
                // 今日成交额
                dayOrderAmt = dayOrderAmt.add(payPrice);
            }

            // 今日成交量
            dayOrderNum = orderVos.size();
        }

        // 根据日期时间查询结算数据，获取今日结算金额，今日结算佣金
        OrderSettleBillDto orderSettleBillDto = new OrderSettleBillDto();
        orderSettleBillDto.setStartTradeTime(startTime);
        orderSettleBillDto.setEndTradeTime(endTime);
        List<OrderSettleBillVo> orderSettleBillVos = orderSettleBillApi.listByDto(orderSettleBillDto).unpack();
        if (!CollectionUtils.isEmpty(orderSettleBillVos)) {
            for (OrderSettleBillVo orderSettleBillVo : orderSettleBillVos) {
                BigDecimal tradeAmt = orderSettleBillVo.getTradeAmt();
                // 交易类型(1-商品本金,2-商品补贴,3-佣金)
                Integer tradeType = orderSettleBillVo.getTradeType();
                if (OrderSettleBillTradeTypeEnum.COMMISSION_AMT.getValue().equals(tradeType)) {
                    // 今日结算佣金
                    daySettleSubsidyAmt = daySettleSubsidyAmt.add(tradeAmt);
                } else if (OrderSettleBillTradeTypeEnum.MERCHANT_AMT.getValue().equals(tradeType)
                        || OrderSettleBillTradeTypeEnum.SUBSIDY_AMT.getValue().equals(tradeType)) {
                    // 今日结算金额=今天完成结算的订单金额总和（包含本金+补贴）；
                    daySettleAmt = daySettleAmt.add(tradeAmt);
                }
            }
        }

        //  今日提现佣金
        WithdrawalBillDto withdrawalBillDto = new WithdrawalBillDto();
        withdrawalBillDto.setWithdrawalType(WithdrawalTypeEnum.AGENT_COMMISSION.getValue());
        withdrawalBillDto.setState(WithdrawalStateEnum.WITHDRAWAL_SUCCESS.getValue());
        withdrawalBillDto.setArriveTimeStart(startTime);
        withdrawalBillDto.setArriveTimeEnd(endTime);
        List<WithdrawalBillVo> withdrawalBillVos = withdrawalBillApi.listByDto(withdrawalBillDto).unpack();
        if (!CollectionUtils.isEmpty(withdrawalBillVos)) {
            for (WithdrawalBillVo withdrawalBillVo : withdrawalBillVos) {
                BigDecimal tradeAmt = withdrawalBillVo.getTradeAmt();
                if (tradeAmt != null && tradeAmt.compareTo(BigDecimal.ZERO) > 0) {
                    dayWithdrawalAmt = dayWithdrawalAmt.add(tradeAmt);
                }
            }
        }

        // 今日新增用户数
        newUserNum = appUserApi.getNewCountByDate(startTime, endTime).unpack();

        // 今日新增商家数
        newBusinessNum = businessApi.getNewCountByDate(startTime, endTime).unpack();

        // 今日新增商户数
        newMerchantNum = merchantApi.getNewCountByDate(startTime, endTime).unpack();

        // 今日新增经纪人数
        newAgentNum = businessAgentApi.getNewCountByDate(startTime, endTime).unpack();

        // 累计发布活动数
        totalActiveNum = activeApi.getCountByDate("", "").unpack();

        // 本周开始日期
        String mondayStartStr = LocalDateUtil.toLocalDateToString(now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)), Constant.YYYY_MM_DD) + " 00:00:00";
        // 本周结束日期
        String sundayEndStr = LocalDateUtil.toLocalDateToString(now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)), Constant.YYYY_MM_DD) + " 23:59:59";
        // 本周上新活动数
        weekActiveNum = activeApi.getCountByDate(mondayStartStr, sundayEndStr).unpack();

        IndexDataStatisticsVo indexDataStatisticsVo = new IndexDataStatisticsVo();
        indexDataStatisticsVo.setDayOrderAmt(dayOrderAmt);
        indexDataStatisticsVo.setDayOrderNum(dayOrderNum);
        indexDataStatisticsVo.setDaySettleAmt(daySettleAmt);
        indexDataStatisticsVo.setDaySettleSubsidyAmt(daySettleSubsidyAmt);
        indexDataStatisticsVo.setDayWithdrawalAmt(dayWithdrawalAmt);
        indexDataStatisticsVo.setNewUserNum(newUserNum);
        indexDataStatisticsVo.setNewBusinessNum(newBusinessNum);
        indexDataStatisticsVo.setNewMerchantNum(newMerchantNum);
        indexDataStatisticsVo.setNewAgentNum(newAgentNum);
        indexDataStatisticsVo.setTotalActiveNum(totalActiveNum);
        indexDataStatisticsVo.setWeekActiveNum(weekActiveNum);

        return indexDataStatisticsVo;
    }

    public List<OrderDataTrendStatisticsVo> orderDataTrendStatistics(OrderDataTrendStatisticsDto orderDataTrendStatisticsDto) {
        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = orderDataTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            orderDataTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            orderDataTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            orderDataTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            orderDataTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            orderDataTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            orderDataTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = orderDataTrendStatisticsDto.getStartTime();
            String endTime = orderDataTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            orderDataTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            orderDataTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        // 成交额
        Map<String, BigDecimal> amtMap = new HashMap<>();

        // 查询成交额（订单金额，实付金额）
        OrderDto orderDto = new OrderDto();
        orderDto.setStartOrderTime(orderDataTrendStatisticsDto.getStartTime());
        orderDto.setEndOrderTime(orderDataTrendStatisticsDto.getEndTime());
        orderDto.setOrderStates(Arrays.asList(OrderStateEnum.WAIT_VERIFY.getValue(), OrderStateEnum.VERIFYING.getValue(),
                OrderStateEnum.FINISH.getValue(), OrderStateEnum.VERIFIED.getValue()));
        List<OrderVo> orderVos = orderApi.listByDto(orderDto).unpack();
        if (!CollectionUtils.isEmpty(orderVos)) {
            for (OrderVo orderVo : orderVos) {
                // 实付金额
                BigDecimal payPrice = orderVo.getPayPrice();
                // 下单时间
                LocalDateTime orderTime = orderVo.getOrderTime();
                String mapKey = getMapKey(dateType, orderTime);
                BigDecimal amt = amtMap.get(mapKey);
                if (amt == null) {
                    amt = BigDecimal.ZERO;
                }
                amt = amt.add(payPrice);
                amtMap.put(mapKey, amt);
            }
        }

        // 成交量（订单数）
        Map<String, List<OrderItemVo>> map = new HashMap<>();

        OrderItemDto orderItemDto = new OrderItemDto();
        orderItemDto.setStartOrderTime(orderDataTrendStatisticsDto.getStartTime());
        orderItemDto.setEndOrderTime(orderDataTrendStatisticsDto.getEndTime());
        orderItemDto.setSubOrderStates(Arrays.asList(SubOrderStateEnum.WAIT_VERIFY.getValue(), SubOrderStateEnum.VERIFIED.getValue()));
        List<OrderItemVo> orderItemVos = orderItemApi.listByDto(orderItemDto).unpack();
        if (!CollectionUtils.isEmpty(orderItemVos)) {
            for (OrderItemVo orderItemVo : orderItemVos) {
                // 下单时间
                LocalDateTime orderTime = orderItemVo.getOrderTime();
                String mapKey = getMapKey(dateType, orderTime);
                List<OrderItemVo> orderItems = map.get(mapKey);
                if (CollectionUtils.isEmpty(orderItems)) {
                    orderItems = new ArrayList<>();
                }
                orderItems.add(orderItemVo);
                map.put(mapKey, orderItems);
            }
        }

        List<OrderDataTrendStatisticsVo> orderDataTrendStatisticsVos = new ArrayList<>();
        if (dateType == 1) {
            for (int i = 0; i < 24; i++) {
                int orderNum = 0;
                String date = i < 10 ? ("0" + i) : String.valueOf(i);

                BigDecimal orderAmt = amtMap.get(date);
                if (orderAmt == null) {
                    orderAmt = BigDecimal.ZERO;
                }

                List<OrderItemVo> orderItems = map.get(date);
                if (!CollectionUtils.isEmpty(orderItems)) {
                    orderNum = orderItems.stream().map(OrderItemVo::getOrderId).collect(Collectors.toSet()).size();
                }

                OrderDataTrendStatisticsVo orderDataTrendStatisticsVo = new OrderDataTrendStatisticsVo();
                orderDataTrendStatisticsVo.setDate(date);
                orderDataTrendStatisticsVo.setOrderAmt(orderAmt.toString());
                orderDataTrendStatisticsVo.setOrderNum(String.valueOf(orderNum));
                orderDataTrendStatisticsVos.add(orderDataTrendStatisticsVo);
            }
        } else {
            LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(orderDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
            LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(orderDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
            for (int i = 0; i < days; i++) {
                int orderNum = 0;
                String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);

                BigDecimal orderAmt = amtMap.get(date);
                if (orderAmt == null) {
                    orderAmt = BigDecimal.ZERO;
                }

                List<OrderItemVo> orderItems = map.get(date);
                if (!CollectionUtils.isEmpty(orderItems)) {
                    orderNum = orderItems.stream().map(OrderItemVo::getOrderId).collect(Collectors.toSet()).size();
                }

                OrderDataTrendStatisticsVo orderDataTrendStatisticsVo = new OrderDataTrendStatisticsVo();
                orderDataTrendStatisticsVo.setDate(date);
                orderDataTrendStatisticsVo.setOrderAmt(orderAmt.toString());
                orderDataTrendStatisticsVo.setOrderNum(String.valueOf(orderNum));
                orderDataTrendStatisticsVos.add(orderDataTrendStatisticsVo);

                startLocalDate = startLocalDate.plusDays(1);
            }
        }

        return orderDataTrendStatisticsVos;
    }

    public List<ActiveDataTrendStatisticsVo> activeDataTrendStatistics(ActiveDataTrendStatisticsDto activeDataTrendStatisticsDto) {
        // 日期类型：1-今日，2-本周，3-本月，4-自定义
        Integer dateType = activeDataTrendStatisticsDto.getDateType();
        if (dateType == null) {
            Throw.isBusinessException("请选择查询日期类型");
        }

        LocalDate nowLocalDate = LocalDate.now();
        if (dateType == 1) {
            String nowDateStr = LocalDateUtil.toLocalDateToString(nowLocalDate, Constant.YYYY_MM_DD);

            activeDataTrendStatisticsDto.setStartTime(nowDateStr + " 00:00:00");
            activeDataTrendStatisticsDto.setEndTime(nowDateStr + " 23:59:59");
        } else if (dateType == 2) {
            // 本周开始日期
            LocalDate monday = nowLocalDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            // 本周结束日期
            LocalDate sunday = nowLocalDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

            String mondayStartStr = LocalDateUtil.toLocalDateToString(monday, Constant.YYYY_MM_DD);
            String sundayEndStr = LocalDateUtil.toLocalDateToString(sunday, Constant.YYYY_MM_DD);

            activeDataTrendStatisticsDto.setStartTime(mondayStartStr + " 00:00:00");
            activeDataTrendStatisticsDto.setEndTime(sundayEndStr + " 23:59:59");
        } else if (dateType == 3) {
            // 本月1号
            LocalDate firstDayOfMonth = nowLocalDate.with(TemporalAdjusters.firstDayOfMonth());
            // 本月最后一天
            LocalDate lastDayOfMonth = nowLocalDate.with(TemporalAdjusters.lastDayOfMonth());

            String firstDayOfMonthStartStr = LocalDateUtil.toLocalDateToString(firstDayOfMonth, Constant.YYYY_MM_DD);
            String lastDayOfMonthEndStr = LocalDateUtil.toLocalDateToString(lastDayOfMonth, Constant.YYYY_MM_DD);

            activeDataTrendStatisticsDto.setStartTime(firstDayOfMonthStartStr + " 00:00:00");
            activeDataTrendStatisticsDto.setEndTime(lastDayOfMonthEndStr + " 23:59:59");
        } else if (dateType == 4) {
            String startTime = activeDataTrendStatisticsDto.getStartTime();
            String endTime = activeDataTrendStatisticsDto.getEndTime();
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                Throw.isBusinessException("请选择查询日期");
            }

            LocalDate startLocalDate = null;
            try {
                startLocalDate = LocalDateUtil.toLocalDate(startTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询开始日期格式错误");
            }

            LocalDate endLocalDate = null;
            try {
                endLocalDate = LocalDateUtil.toLocalDate(endTime, Constant.YYYY_MM_DD);
            } catch (Exception e) {
                Throw.isBusinessException("查询结束日期格式错误");
            }

            // 自定义日期最长不超过3个月
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
            if (days > 90) {
                Throw.isBusinessException("自定义日期最长不超过90天");
            }

            activeDataTrendStatisticsDto.setStartTime(startTime + " 00:00:00");
            activeDataTrendStatisticsDto.setEndTime(endTime + " 23:59:59");
        }

        //  今日 24小时  3或4小时一条
        //  本周  7天  按天
        //  其他日期按天（只能选到天））  最大不超过3个月
        // 根据日期类型判断是根据小时分组还是日期
//        String key = dateType == 1 ? "HH" : "yyyy-MM-dd";

        Map<String, Integer> map = new HashMap<>();

        ActiveDto activeDto = new ActiveDto();
        activeDto.setStartReleaseTime(activeDataTrendStatisticsDto.getStartTime());
        activeDto.setEndReleaseTime(activeDataTrendStatisticsDto.getEndTime());
        activeDto.setActiveStates(Arrays.asList(ActiveStateEnum.NO_START.getValue(), ActiveStateEnum.IN_PROGRESS.getValue(), ActiveStateEnum.ENDED.getValue()));
        activeDto.setDelState(0);
        List<ActivePageVo> activePageVos = activeApi.listByDto(activeDto).unpack();
        if (!CollectionUtils.isEmpty(activePageVos)) {
            for (ActivePageVo activePageVo : activePageVos) {
                // 下单时间
                LocalDateTime releaseTime = activePageVo.getReleaseTime();
                String mapKey = getMapKey(dateType, releaseTime);

                Integer num = map.get(mapKey);
                if (num == null) {
                    num = 0;
                }
                num += 1;
                map.put(mapKey, num);
            }
        }

        List<ActiveDataTrendStatisticsVo> activeDataTrendStatisticsVos = new ArrayList<>();
        if (dateType == 1) {
            for (int i = 0; i < 24; i++) {
                String date = i < 10 ? ("0" + i) : String.valueOf(i);
                Integer num = map.get(date);
                if (num == null) {
                    num = 0;
                }

                ActiveDataTrendStatisticsVo activeDataTrendStatisticsVo = new ActiveDataTrendStatisticsVo();
                activeDataTrendStatisticsVo.setDate(date);
                activeDataTrendStatisticsVo.setValue(String.valueOf(num));
                activeDataTrendStatisticsVos.add(activeDataTrendStatisticsVo);
            }
        } else {
            LocalDate startLocalDate = LocalDateUtil.toLocalDateTime(activeDataTrendStatisticsDto.getStartTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
            LocalDate endLocalDate = LocalDateUtil.toLocalDateTime(activeDataTrendStatisticsDto.getEndTime(), Constant.YYYY_MM_DD_HH_MM_SS).toLocalDate();
            long days = ChronoUnit.DAYS.between(startLocalDate, endLocalDate) + 1;
            for (int i = 0; i < days; i++) {
                String date = LocalDateUtil.toLocalDateToString(startLocalDate, Constant.YYYY_MM_DD);

                Integer num = map.get(date);
                if (num == null) {
                    num = 0;
                }

                ActiveDataTrendStatisticsVo activeDataTrendStatisticsVo = new ActiveDataTrendStatisticsVo();
                activeDataTrendStatisticsVo.setDate(date);
                activeDataTrendStatisticsVo.setValue(String.valueOf(num));
                activeDataTrendStatisticsVos.add(activeDataTrendStatisticsVo);

                startLocalDate = startLocalDate.plusDays(1);
            }
        }

        return activeDataTrendStatisticsVos;
    }

    private String getMapKey(Integer dateType, LocalDateTime releaseTime) {
        String mapKey;
        if (dateType == 1) {
            int hour = releaseTime.getHour();
            mapKey = hour > 9 ? String.valueOf(hour) : "0" + hour;
        } else {
            mapKey = LocalDateUtil.toLocalDateToString(releaseTime.toLocalDate(), Constant.YYYY_MM_DD);
        }
        return mapKey;
    }

    public TodoDataVo toDoDataStatistics() {
        // 商家入驻审核个数
        int businessEnterAuditNum = 0;
        // 商户本金提现审核个数
        int merchantAmtWithdrawalAuditNum = 0;
        // 商户补贴提现审核个数
        int merchantSubsidyWithdrawalAuditNum = 0;
        // 经纪人提现审核个数
        int agentWithdrawalAuditNum = 0;

        // 查询商家入驻审核个数
        BusinessEnterApplyQueryDto businessEnterApplyQueryDto = new BusinessEnterApplyQueryDto();
        businessEnterApplyQueryDto.setAuditStates(Collections.singletonList(AuditStateEnum.WAIT_AUDIT.getValue()));
        businessEnterApplyQueryDto.setSystemAuditStates(Collections.singletonList(SystemAuditStateEnum.MANAGER_WAIT_AUDIT.getValue()));
        List<BusinessEnterApplyPageVo> businessEnterApplyVos = businessEnterApplyApi.listByDto(businessEnterApplyQueryDto).unpack();
        if (!CollectionUtils.isEmpty(businessEnterApplyVos)) {
            businessEnterAuditNum = businessEnterApplyVos.size();
        }

        // 查询待审核的提现记录数据
        WithdrawalBillDto withdrawalBillDto = new WithdrawalBillDto();
        withdrawalBillDto.setState(WithdrawalStateEnum.WAIT_AUDIT.getValue());
        List<WithdrawalBillVo> withdrawalBills = withdrawalBillApi.listByDto(withdrawalBillDto).unpack();
        if (!CollectionUtils.isEmpty(withdrawalBills)) {
            Map<Integer, List<WithdrawalBillVo>> merchantWithdrawalBillVos = withdrawalBills.stream().collect(Collectors.groupingBy(WithdrawalBillVo::getWithdrawalType));

            // 商户本金提现审核个数
            List<WithdrawalBillVo> withdrawalBillVos = merchantWithdrawalBillVos.get(WithdrawalTypeEnum.MERCHANT_AMT.getValue());
            if (!CollectionUtils.isEmpty(withdrawalBillVos)) {
                merchantAmtWithdrawalAuditNum = withdrawalBillVos.size();
            }

            // 商户补贴提现审核个数
            withdrawalBillVos = merchantWithdrawalBillVos.get(WithdrawalTypeEnum.MERCHANT_SUBSIDY.getValue());
            if (!CollectionUtils.isEmpty(withdrawalBillVos)) {
                merchantSubsidyWithdrawalAuditNum = withdrawalBillVos.size();
            }

            // 经纪人提现审核个数
            withdrawalBillVos = merchantWithdrawalBillVos.get(WithdrawalTypeEnum.AGENT_COMMISSION.getValue());
            if (!CollectionUtils.isEmpty(withdrawalBillVos)) {
                agentWithdrawalAuditNum = withdrawalBillVos.size();
            }
        }

        TodoDataVo todoDataVo = new TodoDataVo();
        todoDataVo.setBusinessEnterAuditNum(businessEnterAuditNum);
        todoDataVo.setMerchantAmtWithdrawalAuditNum(merchantAmtWithdrawalAuditNum);
        todoDataVo.setMerchantSubsidyWithdrawalAuditNum(merchantSubsidyWithdrawalAuditNum);
        todoDataVo.setAgentWithdrawalAuditNum(agentWithdrawalAuditNum);

        return todoDataVo;
    }
}
